package net.lqy.aipan.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import net.lqy.aipan.dto.AccountDTO;
import net.lqy.aipan.interceptor.LoginInterceptor;
import net.lqy.aipan.request.AccountLoginReq;
import net.lqy.aipan.request.AccountRegisterReq;
import net.lqy.aipan.service.AccountService;
import net.lqy.aipan.util.JsonData;
import net.lqy.aipan.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @title: AccountController
 * @Author lqy
 * @Date: 2025-06-02 11:52
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/v1/account")
@Tag(name = "账户管理", description = "用户注册、登录、头像上传及获取用户详情信息接口")
public class AccountController {
    @Autowired
     private AccountService accountService;

    /**
     * 用户注册
     */
     @PostMapping("/register")
     @Operation(summary = "用户注册", description = "根据提供的注册信息创建新用户")
      public JsonData register(@RequestBody AccountRegisterReq request) {
          return accountService.register(request);
     }

    /**
     * 头像上传
     */
     @PostMapping("/upload_avatar")
     @Operation(summary = "上传头像", description = "上传用户的头像图片并返回访问地址")
       public JsonData uploadAvatar(@RequestParam("file") MultipartFile file) {
         String url=accountService.uploadAvatar(file);
         return JsonData.buildSuccess(url);
     }

    /**
     * 账号密码登录
     */
     @PostMapping("/login")
     @Operation(summary = "账号密码登录", description = "用户通过账号和密码进行登录，并获取 JWT token")
       public JsonData login(@RequestBody AccountLoginReq request) {
         AccountDTO accountDTO=accountService.login(request);

         //生成token jwt
          String token= JwtUtil.geneLoginJWT(accountDTO);
          return JsonData.buildSuccess(token);
     }

    /**
     * 获取用户详情信息
     */
    @GetMapping("/detail")
    @Operation(summary = "获取用户详情", description = "获取当前登录用户的详细信息")
    public JsonData detail() {

        AccountDTO accountDTO= accountService.detail(LoginInterceptor.threadLocal.get().getId());
        if (accountDTO==null){
            return JsonData.buildError("用户不存在");
        }
        return JsonData.buildSuccess(accountDTO);
    }
}
